package com.kristou.urgdroid;

import android.app.Activity;
import android.app.Fragment;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.kristou.urgdriver.connection.SerialConnection;
import com.kristou.urgdriver.sensor.RangeSensorInformation;
import com.kristou.urgdriver.sensor.UrgDevice;
import com.kristou.urgdriver.service.CaptureService;

import java.lang.ref.WeakReference;

/**
 * Created by Kristou on 13/09/26.
 */
public class ConnectionFragment extends Fragment {

    private static final String TAG = ConnectionFragment.class.getSimpleName();

    private View mView;

    private Button mBtnUpdate;

    private TextView txtName;
    private TextView txtSerial;
    private TextView txtFirmware;

    private ConnectionFragmentListener listener;

    public interface ConnectionFragmentListener {
        public void deviceConnected(UrgDevice device);

        public void deviceDisconnected();

        public CaptureService getCaptureService();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ConnectionFragmentListener) {
            listener = (ConnectionFragmentListener) activity;
        } else {
            throw new ClassCastException(activity.toString()
                    + " must implemenet ConnectionFragment.onConnectionListener");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_connection,
                container, false);


        updateInformation();
        getBtnUpdaten().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateInformation();
            }
        });

        return mView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    private void updateInformation() {
        if(listener.getCaptureService() == null) return;

        RangeSensorInformation info = listener.getCaptureService().getInformation();
        if (info != null) {
            getTxtName().setText(info.product);
            getTxtSerial().setText(info.serial_number);
            getTxtFirmware().setText(info.firmware);
        } else {
            getTxtName().setText(R.string.caption_none);
            getTxtSerial().setText(R.string.caption_none);
            listener.deviceDisconnected();
            String msg = "Sensor is disconnected";
            Toast.makeText(this.getActivity(), msg, Toast.LENGTH_LONG).show();
        }
    }
//
//    private static String getStringFromInputStream(InputStream is) {
//
//        BufferedReader br = null;
//        StringBuilder sb = new StringBuilder();
//
//        String line;
//        try {
//
//            br = new BufferedReader(new InputStreamReader(is));
//            while ((line = br.readLine()) != null) {
//                sb.append(line);
//            }
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        } finally {
//            if (br != null) {
//                try {
//                    br.close();
//                } catch (IOException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//
//        return sb.toString();
//    }

//    private void populateBaudrates(Spinner baudrates) {
//        List<String> list = new ArrayList<String>();
//        list.add("19.5Kps");
//        list.add("57.6Kps");
//        list.add("115.0Kps");
//        list.add("230.4Kps");
//        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, list);
//        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//        baudrates.setAdapter(dataAdapter);
//    }
//
//    private void populatePorts(Spinner ports) {
//        UsbManager manager = (UsbManager) getActivity().getSystemService(Context.USB_SERVICE);
//        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
//        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();
//        List<String> list = new ArrayList<String>();
//
//        while (deviceIterator.hasNext()) {
//            UsbDevice device = deviceIterator.next();
//            if (device.getVendorId() == 5585 &&
//                    device.getProductId() == 0) {
//                list.add(device.getDeviceName());
//            }
//        }
//
//        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, list);
//        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//        ports.setAdapter(dataAdapter);
//    }


    public Button getBtnUpdaten() {
        if (mBtnUpdate == null) {
            mBtnUpdate = (Button) mView.findViewById(R.id.btnUpdate);
        }
        return mBtnUpdate;
    }

    public TextView getTxtName() {
        if (txtName == null) {
            txtName = (TextView) mView.findViewById(R.id.txtName);
        }
        return txtName;
    }

    public TextView getTxtSerial() {
        if (txtSerial == null) {
            txtSerial = (TextView) mView.findViewById(R.id.txtSerial);
        }
        return txtSerial;
    }

    public TextView getTxtFirmware() {
        if (txtFirmware == null) {
            txtFirmware = (TextView) mView.findViewById(R.id.txtFirmware);
        }
        return txtFirmware;
    }
}
